/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Clases.*;
import Persistencia.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author JAVA
 */
public class SEditar extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        String clase = request.getParameter("id");
        
        if(clase.equals("Cliente")){
            
            // RECOGIDA DEL CLIENTE Y ASIGNACION DE ATRIBUTO            
            Cliente cliente = PersistenciaCliente.obtenerCliente(request.getParameter("codigo"));
            request. setAttribute("cliente", cliente);
            
            // REDIRECCION A EDITARCLIENTE
            RequestDispatcher rd = request.getRequestDispatcher("editaCliente.jsp");
            rd.forward(request, response);
            
        }else if(clase.equals("FacClientes")){
            
            // RECOGIDA DEL DATO DE FACUTRACION Y ASIGNACION DE ATRIBUTO            
            FacturacionCliente facCliente = PersistenciaFacturacionCliente.obtenerFacturacionCliente(request.getParameter("codigo"), request.getParameter("nifV"));
            request. setAttribute("faccliente", facCliente);
            
            // REDIRECCION A EDITADATOSFACTURACION
            RequestDispatcher rd = request.getRequestDispatcher("editaDatosFacturacion.jsp");
            rd.forward(request, response);
        
        }else if(clase.equals("SFacturacion")){
            
            // RECOGIDA DEL DATO DE SERIE DE FACUTRACION Y ASIGNACION DE ATRIBUTO            
            SFacturacion sfac = PersistenciaSFacturacion.obtenerSFacturacion(request.getParameter("serie"));
            request. setAttribute("sfacturacion", sfac);
            
            // REDIRECCION A EDITASFACTURACION
            RequestDispatcher rd = request.getRequestDispatcher("editaSfacturacion.jsp");
            rd.forward(request, response);
            
        }else if(clase.equals("Servicio")){
            
             // RECOGIDA DEL DATO DE SERVICIO Y ASIGNACION DE ATRIBUTO            
            Servicio servicio = PersistenciaServicio.obtenerServicio(request.getParameter("nombre"));
            request. setAttribute("servicio", servicio);
            
            // REDIRECCION A EDITASERVICIO
            RequestDispatcher rd = request.getRequestDispatcher("editaServicio.jsp");
            rd.forward(request, response);
            
        }else if(clase.equals("TipoIva")){
            
             // RECOGIDA DEL DATO DE TIPO DE IVA Y ASIGNACION DE ATRIBUTO            
            TiposIva tipoiva = PersistenciaTiposIva.obtenterTipoIva(request.getParameter("codigo"));
            request. setAttribute("tipoiva", tipoiva);
            
            // REDIRECCION A EDITATIPOIVA
            RequestDispatcher rd = request.getRequestDispatcher("editaTipoIva.jsp");
            rd.forward(request, response);
            
        }else if(clase.equals("Usuario")){
            
        }else if(clase.equals("Gasto")){
            Gasto gasto = PersistenciaGasto.obtenterGasto(request.getParameter("codigo"));
            request.setAttribute("gasto", gasto);

            RequestDispatcher rd = request.getRequestDispatcher("editaGasto.jsp");
            rd.forward(request, response);
            
        }else if(clase.equals("FacturaE"))
        {
            Factura factura = PersistenciaFactura.obtenerFactura(request.getParameter("codigo"));
            request.setAttribute("factura", factura);
            
            RequestDispatcher rd = request.getRequestDispatcher("editaFactura.jsp");
            rd.forward(request, response);
        }
      
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SEditar</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SEditar at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(SEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(SEditar.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
